package com.cloud.school.bus.teacherhelper.modules.home.adapter;

import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.universalimageloader.core.ImageLoader;
import com.android.support.jhf.universalimageloader.core.assist.FailReason;
import com.android.support.jhf.universalimageloader.core.listener.ImageLoadingListener;
import com.android.support.jhf.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.android.support.jhf.utils.ImageUtil;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.TeacherHelperApplication;
import com.cloud.school.bus.teacherhelper.entitys.HomeMediaFolder;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Debug;
import android.provider.MediaStore.Video.Thumbnails;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

/**
 * 首页图片视频文件夹列表
 * 
 * @author lenovo
 * 
 */
public class HomeAdapter extends BaseAdapter {

	private Context mContext;
	private TeacherHelperApplication mTeacherHelperApplication;
	private List<HomeMediaFolder> mHomeMediaFolders;

	public HomeAdapter(Context context, List<HomeMediaFolder> homeMediaFolders) {
		mContext = context;
		mTeacherHelperApplication = (TeacherHelperApplication) mContext
				.getApplicationContext();
		mHomeMediaFolders = homeMediaFolders;
	}

	@Override
	public int getCount() {
		return mHomeMediaFolders.size();
	}

	@Override
	public Object getItem(int position) {
		return mHomeMediaFolders.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (null == convertView) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.adapter_home, null);
		}

		HomeMediaFolder homeMediaFolder = mHomeMediaFolders.get(position);

		ImageView mediaImageView = (ImageView) convertView
				.findViewById(R.id.mediaImageView);
		mediaImageView.setImageResource(homeMediaFolder.defaultImage);
		if (!TextUtils.isEmpty(homeMediaFolder.mediaPath)) {
			if (homeMediaFolder.mediaPath.endsWith("MP4")
					|| homeMediaFolder.mediaPath.endsWith("mp4")) {
				// Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(
				// homeMediaFolder.mediaPath.replace("file:///", "/"),
				// Thumbnails.MINI_KIND);
//				if (null != bitmap) {
//					mediaImageView.setImageBitmap(bitmap);
//				}else {
					mediaImageView.setImageResource(R.drawable.ic_default_video);
//				}
			} else {
				ImageLoader.getInstance().displayImage(
						homeMediaFolder.mediaPath, mediaImageView,
						mTeacherHelperApplication.mNoCacheDisplayImageOptions,
						new SimpleImageLoadingListener() {
							@Override
							public void onLoadingComplete(String imageUri,
									final View view, final Bitmap loadedImage) {
								ImageView imageView = (ImageView) view;

								if (imageUri.startsWith("http://")) {
									imageView.setImageBitmap(loadedImage);
								} else if (imageUri.startsWith("file:///")) {
									ImageUtil.setRotaingImageBitmap(
											imageUri.replace("file:///", "/"),
											loadedImage, imageView);
								} else {
									ImageUtil.setRotaingImageBitmap(imageUri,
											loadedImage, imageView);
								}

								// imageView.setImageBitmap(loadedImage);

							}
						});
			}
		} else {
			mediaImageView.setImageResource(homeMediaFolder.defaultImage);
		}

		TextView mediaNameTextView = (TextView) convertView
				.findViewById(R.id.mediaNameTextView);
		mediaNameTextView.setText(homeMediaFolder.folder);

		return convertView;
	}
}
